package com.example.object.game;

import android.content.Context;

import com.example.object.GameShape;

import org.jbox2d.dynamics.World;

/**
 * Created by Lucas on 06/07/13.
 */
public class Chain extends GameShape {

    private Anchor _anchorLeft;
    private Anchor _anchorRight;

    private Elastic elastic[];

    private final String TAG = "Chain";

    public Chain(Context context, float xCenter, float yCenter, float halfSize){
        _handlerInitiate = false;
        _anchorLeft = new Anchor(context, yCenter, xCenter-halfSize, 0.05f);
        _anchorRight = new Anchor(context, yCenter, xCenter+halfSize, 0.05f);
        elastic = new Elastic[5];
        elastic[2] = new Elastic(context, xCenter, yCenter, halfSize*0.1f, 0.02f);
        elastic[0] = new Elastic(context, yCenter, xCenter-halfSize*0.55f, halfSize*0.25f, 0.02f);
        elastic[4] = new Elastic(context, yCenter, xCenter+halfSize*0.55f, halfSize*0.25f, 0.02f);
        elastic[1] = new Elastic(context, yCenter, xCenter-halfSize*0.05f, halfSize*0.25f, 0.02f);
        elastic[3] = new Elastic(context, yCenter, xCenter+halfSize*0.05f, halfSize*0.25f, 0.02f);

    }

    /**
     * Draw the shape into the GLSurfaceScreen
     */
    @Override
    public void draw() {
        _anchorLeft.draw();
        _anchorRight.draw();
        for(int i=0; i<5; i++){
            elastic[i].draw();
        }
    }

    /**
     * return a boolean to know if the object is movable
     *
     * @return false
     */
    @Override
    public boolean isMovable() {
        return true;
    }

    @Override
    public void calculateNextCoordinate() {
        for(int i=0; i<elastic.length; i++){
            elastic[i].calculateNextCoordinate();
        }
    }

    /**
     * set the OpenGL resource to the openGL Object
     *
     * @param aPositionHandle
     * @param aTextureHandle
     * @param uMVPMatrixHandle
     * @param vMatrix
     * @param projMatrix
     */
    @Override
    public void setGLHandler(int aPositionHandle, int aTextureHandle, int uMVPMatrixHandle, float[] vMatrix, float[] projMatrix) {
        for(int i=0; i<5; i++){
            elastic[i].setGLHandler(aPositionHandle, aTextureHandle, uMVPMatrixHandle, vMatrix, projMatrix);
        }
        _anchorRight.setGLHandler(aPositionHandle, aTextureHandle, uMVPMatrixHandle, vMatrix, projMatrix);
        _anchorLeft.setGLHandler(aPositionHandle, aTextureHandle, uMVPMatrixHandle, vMatrix, projMatrix);
        _handlerInitiate = true;
    }

    @Override
    public void addToWorld(World world) {
        for(int i=0; i<5; i++){
            elastic[i].addToWorld(world);
        }
        _anchorLeft.addToWorld(world);
        _anchorRight.addToWorld(world);
        elastic[0].jointTo(_anchorLeft, world);
        elastic[4].jointTo(_anchorRight, world);
        elastic[0].jointTo(elastic[1], world);
        elastic[1].jointTo(elastic[2], world);
        elastic[2].jointTo(elastic[3], world);
        elastic[3].jointTo(elastic[4], world);
    }
}
